﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using MultipleChoice_ExamGenerator.VariousUtils;
using MySql.Data.MySqlClient;

namespace MultipleChoice_ExamGenerator
{
    public partial class EditProfile : Form
    {
        private readonly AuthorValidator _authorValidator;

        public EditProfile()
        {
            InitializeComponent();
            _authorValidator = new AuthorValidator();
        }


        private void EditProfile_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'multiplechoicedbDataSet.authors' table. You can move, or remove it, as needed.
            this.authorsTableAdapter.FillByAuthorID(this.multiplechoicedbDataSet.authors, ActiveAuthor.GetCurrentAuthor());
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (!IsValid())
            {
                MessageBox.Show("Please enter valid data!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                this.Validate();
                this.authorsBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(this.multiplechoicedbDataSet);
                MessageBox.Show("Profile Modified Successful!", "",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                Close();
            }
            catch (MySqlException mySqlException)
            {
                MessageBox.Show(mySqlException.Message,
                "Error!",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Με αυτή την μέθοδο ελέγχουμε αν τα πεδία της φόρμας
        /// είναι σωστά συμπληρωμένα
        /// </summary>
        /// <returns>true/false</returns>
        private bool IsValid()
        {
            return _authorValidator.ValidateGender(genderComboBox, errorProvider1)
                & _authorValidator.ValidateSecurityQuestion(securityQuestionComboBox, errorProvider1)
                & _authorValidator.ValidatePassword(passwordTextBox, errorProvider1)
                & _authorValidator.ValidateEmail(emailTextBox, errorProvider1)
                & _authorValidator.ValidateRequired(firstnameTextBox, errorProvider1)
                & _authorValidator.ValidateRequired(lastnameTextBox, errorProvider1)
                & _authorValidator.ValidateRequired(secutityAnswerTextBox, errorProvider1);
        }

        private void passwordTextBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidatePassword(passwordTextBox, errorProvider1);
        }

        private void firstnameTextBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(firstnameTextBox, errorProvider1);
        }

        private void lastnameTextBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(lastnameTextBox, errorProvider1);
        }

        private void emailTextBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateEmail(emailTextBox, errorProvider1);
        }

        private void securityQuestionComboBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateSecurityQuestion(securityQuestionComboBox, errorProvider1);
        }

        private void secutityAnswerTextBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateRequired(secutityAnswerTextBox, errorProvider1);
        }

        private void genderComboBox_Validating(object sender, CancelEventArgs e)
        {
            _authorValidator.ValidateGender(genderComboBox, errorProvider1);
        }
    }
}
